<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mail_model extends CI_Model {
	//모델 생성자 호출
	function __construct(){
		parent::__construct();
	
	}
	
	/**
	 * 메일 대상자 카운트 조회
	 *
	 * @access	public
	 * @return	array
	 */
	function select_count_target(){
		$q = "SELECT (SELECT COUNT(*) FROM ch_member WHERE mailling_yn='Y') AS total,";
		$q .= "(SELECT COUNT(*) FROM ch_member WHERE mailling_yn='Y' AND substring(birth_date,6,5)='".date('m-d')."') AS birthday";
		$query = $this->db->query($q);
		$row = $query->row_array();		
		//echo $this->db->last_query();
		$data = array('total'=> $row['total'],'birthday'=>$row['birthday']);
	  	return $data;
	}
	
	/**
	 * 메일 대상자 조회
	 *
	 * @access	public
	 * @param   array
	 * @return	result
	 */
	function select_target($where){
		$this->db->where($where);
		$this->db->order_by('id DESC');
		$query = $this->db->get('ch_member');
		//echo $this->db->last_query();
		return $query->result();
	}
	
	/**
	 * 메일 내용
	 *
	 * @access	public
	 * @return	string
	 */
	function get_mail_body(){
		$template = null;
		$content = $this->input->post('content');
		if($this->input->post('template')){
			$template = read_file('./template/mail/'.$this->input->post('template').'/mail.html');
			$content = str_replace('{CONTENT}',$content, $template);
			$content = str_replace('{HTTP_URL}',base_url(),$content);
		}
		return $content;
	}
}
?>